#!/usr/bin/perl

# this has been fixed to look for missing .anm.p files
# Ryan 2003/11/21

use strict;

use IO::Dir;
use IO::File;

main();
exit;

sub main {
  my $dir = IO::Dir->new('.');

  if (!$dir) {
    die "Huh?? can't open '.' to get a directory listing!\nError: $!\n";
  }

  my $dir_entry;

  while ($dir_entry = $dir->read) {
    next if $dir_entry =~ /^\.\.?$/;
    next if $dir_entry =~ /^CVS/i;
    next if ! -d $dir_entry;
    verify_flickers($dir_entry);
  }

  $dir->close;

  print "Hit enter to exit\n";
  <stdin>;
}

sub verify_flickers {
  my ($dirname) = @_;

  my $flicker_dir = IO::Dir->new($dirname);

  if (!$flicker_dir) {
    print "Huh?? can't open '$dirname' to get a directory listing!\nError:$!\n";
    return;
  }

  my $dir_entry;
  my %codes = (
    'rds' => '',
    'rdw' => '',
    'rns' => '',
    'rnw' => '',
    'lds' => '',
    'ldw' => '',
    'lns' => '',
    'lnw' => '',
  );
  my %sizes = (
    'rds' => 0,
    'rdw' => 0,
    'rns' => 0,
    'rnw' => 0,
    'lds' => 0,
    'ldw' => 0,
    'lns' => 0,
    'lnw' => 0,
   );

  print "$dirname\n";
  while ($dir_entry = $flicker_dir->read) {
    next if $dir_entry =~ /^\.\.?$/;
    next if -d $dir_entry;
    
    if ($dir_entry =~ /${dirname}_(...)/) {
      my $code = $1;
      $codes{$code} = $dir_entry;
      if ($dir_entry =~ /\.pcx$/) {
        $sizes{$code} += -s "$dirname/$dir_entry";
      }
    }
  }

  for my $code (sort keys %codes) {
    if ($codes{$code} eq '') {
      print "\t", $code, " missing\n";
    } else {
      print "\t", $code, " found (" . $sizes{$code} . " bytes)";

      # its an alias, find out what to
      if ($codes{$code} =~ /\.a$/i) {
        print " (alias to: ";
        my $fh = IO::File->new($dirname . '/' . $codes{$code});
        if (!defined($fh)) {
          print "??";
        } else {
          my $filename = <$fh>;
          chomp($filename);
          print $filename; # print the file its aliased too
          $fh->close;
        }
        print ")";

      # not an alias
      } else {
        $codes{$code} =~ s/\.pcx$//gi;
        $codes{$code} =~ s/\.jpg$//gi;
        $codes{$code} =~ s/\.an1$//gi;
        $codes{$code} =~ s/\.an2$//gi;
        $codes{$code} =~ s/\.anm.p$//gi;
        $codes{$code} =~ s/_[0-9]+//gi;
        my $prop_name = $dirname . '/' . $codes{$code} . ".anm.p";
        if (! (-e $prop_name) ) {
          print " $prop_name missing";
          # create the missing .anm.p file here
          my $fh = IO::File->new($prop_name, "w");
          $fh->print("{}");
          $fh->close;
        }
      }
      print "\n";
    }
  }

  #all done with this flicker dir
  $flicker_dir->close;
}


__END__
